package com.view.cn.mvprxjava.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.view.cn.mvprxjava.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;

import static java.security.AccessController.getContext;

/**
 * 红包分享小功能
 * Created by Zengxiaoping on 2017/2/7.
 */

public class MoneyFragment extends BaseFragment implements View.OnClickListener {

	public static final String TAG = MoneyFragment.class.getSimpleName();
	private EditText mEdMoneyAll;
	private EditText mEdMoneyNumber;
	private TextView mTvInfo;
	private TextView mTvClick;

	public static MoneyFragment newInstance() {
		MoneyFragment fragment = new MoneyFragment();
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_money, container, false);
		initView(view);
		return view;
	}

	private void initView(View view) {
		mEdMoneyAll = (EditText) view.findViewById(R.id.ed_money_all);
		mEdMoneyNumber = (EditText) view.findViewById(R.id.ed_money_number);
		mTvInfo = (TextView) view.findViewById(R.id.tv_info);
		mTvClick = (TextView) view.findViewById(R.id.tv_click);
		mTvClick.setOnClickListener(this);
	}

	private void submit() {
		String allMoneyStr = mEdMoneyAll.getText().toString().trim();
		double allMoney;
		double peosonnumber;
		if (TextUtils.isEmpty(allMoneyStr)) {
			Toast.makeText(getContext(), "allMoney不能为空", Toast.LENGTH_SHORT).show();
			return;
		} else {
			allMoney = Double.valueOf(allMoneyStr);
			if (allMoney <= 0) {
				Toast.makeText(getContext(), "allMoney数据不合法", Toast.LENGTH_SHORT).show();
				return;
			}
		}

		String peosonnumberStr = mEdMoneyNumber.getText().toString().trim();
		if (TextUtils.isEmpty(peosonnumberStr)) {
			Toast.makeText(getContext(), "number不能为空", Toast.LENGTH_SHORT).show();
			return;
		} else {
			peosonnumber = Double.valueOf(peosonnumberStr);
			if (peosonnumber <= 0) {
				Toast.makeText(getContext(), "number数据不合法", Toast.LENGTH_SHORT).show();
				return;
			}
		}

		if ((allMoney * 1.00) / peosonnumber < 0.01) {
			Toast.makeText(getContext(), "数据不合法,请重输入", Toast.LENGTH_SHORT).show();
			return;
		}

		DecimalFormat df = new DecimalFormat("######0.00");
		ArrayList<Double> moneyList = getMoneyList(allMoney, peosonnumber, new Random(), df);
		double sum = 0.00;
		for (double s : moneyList) {
			sum += s;
		}
		mTvInfo.setText(moneyList.toString() + "总合为：" + df.format(sum));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.tv_click:
				submit();
				break;
		}
	}

	public ArrayList<Double> getMoneyList(Double allMoney, Double peosonnumber, Random random, DecimalFormat df) {
		ArrayList<Integer> data = new ArrayList();
		int sumMoney = 0;
		for (int i = 0; i < peosonnumber; i++) {
			int s = random.nextInt(300 - 100) + 100;
			data.add(s);
			sumMoney += s;
		}
		double oneMoney = (allMoney * 1.00) / sumMoney;     //一份拥有的钱
		double sumMoneyDouble = 0.00;
		ArrayList<Double> newData = new ArrayList();
		for (int i = 0; i < data.size(); i++) {
			if (i == data.size() - 1) {
				newData.add(Double.valueOf(df.format(allMoney - sumMoneyDouble)));     //最后一个金额特殊处理，防止遗漏0.01的金额消失
			} else {
				newData.add(Double.valueOf(df.format(oneMoney * data.get(i))));
				sumMoneyDouble += newData.get(i);
			}
		}
		return newData;
	}
}
